package com.example.fairplay.newfpregistration.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.example.fairplay.newfpregistration.R;
import com.example.mylibrary.adapter.RvAdapter;
import com.example.mylibrary.adapter.RvHolder;
import com.example.mylibrary.adapter.RvListener;
import com.example.mylibrary.data.ByteImg;
import com.example.mylibrary.util.BitmapUtil;
import com.example.mylibrary.util.BitmapUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by fairplay on 2018/6/6.
 */

public class StudentPicAdapter extends RvAdapter<ByteImg>{
    private SparseBooleanArray sparseBooleanArray=new SparseBooleanArray();
    public StudentPicAdapter(Context context, List<ByteImg> list, RvListener listener) {
        super(context, list, listener);
    }

    @Override
    protected int getLayoutId(int viewType) {
        return 0;
    }

    @Override
    protected RvHolder getHolder(View view, int viewType) {
        ViewHolder viewHolder=new ViewHolder(view,viewType,listener);
        return viewHolder;
    }

   public class ViewHolder extends RvHolder<ByteImg>{
        CheckBox cb_student_img;
       ImageView iv_student_img;
       public ViewHolder(View itemView, int type, RvListener listener) {
           super(itemView, type, listener);
           cb_student_img=itemView.findViewById(R.id.cb_student_img);
           iv_student_img=itemView.findViewById(R.id.iv_student_img);
       }

       @Override
       public void bindHolder(ByteImg byteImg, int position) {
            byte[] imgs=byteImg.getByteImg();
            Bitmap bitmap= BitmapUtils.byteToBitmap(imgs);
           iv_student_img.setImageBitmap(bitmap);
           cb_student_img.setTag(position);
           cb_student_img.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                   int pos= (int) compoundButton.getTag();
                   if(b){
                       sparseBooleanArray.put(pos,true);
                   }else {
                       sparseBooleanArray.put(pos,false);
                   }
               }
           });
           cb_student_img.setChecked(sparseBooleanArray.get(position));

       }
   }
}
